package com.linyubing.utils;
import java.sql.*;
//封装JDBC公共操作的工具类
public class JDBCUtil {
    public static final String url="jdbc:mysql://127.0.0.1:3306/java_study?useSSL=false";//连接的ip地址
    public static final String username="root";//用户名
    public static final String password="123456";//密码
    public static Connection con=null;//连接对象
    public static PreparedStatement stmt = null ;  //预编译sql对象
    public static ResultSet resultSet=null;
    //获取连接对象
    public static Connection getConnection() throws SQLException {
        //获取连接
        con= DriverManager.getConnection(url,username,password);
        return con;
    }
    //根据传入的sql和连接对象获取预编译sql对象
    public static PreparedStatement getStatement(Connection con,String sql) throws SQLException {
        stmt = con.prepareStatement(sql);
        return stmt;
    }
    //根据传入的stmt获取结果集
    public static ResultSet getResultSet(PreparedStatement stmt) throws SQLException {
        resultSet = stmt.executeQuery();
        return resultSet;
    }
    //关闭连接对象
    public static void closeConnection() throws SQLException {
        if(con!=null){
            con.close();
        }
    }
    //关闭预编译sql对象
    public static void closePreparedStatement() throws SQLException {
       if(stmt!=null){
           stmt.close();
       }
    }
    //关闭resultSet对象
    public static void closeResultSet() throws SQLException {
        if(resultSet!=null){
            resultSet.close();
        }
    }

}
